//作为判断是否打开博客详情标志
let detailsFalg = true;
//热门文章渲染
gethot_onc(6)
//绑定模糊搜索回车
BindSearchKeyup()
//主文章渲染
indexArt()
//绑定我的历史博客
BindHis()
getHead()


document.querySelector('#index_goLogin').onclick = function () {
    console.log(this)
    window.location.href = '../../../index.html'
}

//获取头像
function getHead() {
    future.ajax({
        url: '/users/getHead',
        type: 'post',
        success: function (result) {
            let imgUrl = "../../file/" + result.head;

            document.getElementById("index_top_user_img").src = imgUrl;
            document.getElementById("index_details_discuss_input_title").src = imgUrl;
            document.getElementById("index_top_user_span1").innerText = result.username;
            document.getElementById("index_top_user_span2").innerText = result.mailbox;
            document.getElementById("home_username").innerText = result.username;
            document.getElementById("home_emil").innerText = result.mailbox;
            if (result.username == "undefined") {
                document.getElementById("home_username").innerText = "游客";
                document.getElementById("home_emil").innerText = "开放更多功能请先登录";
            }
        }, error: function () {
            document.getElementById("index_top_user_img").src = " "
            // document.getElementById("hear_img").src=" "
            document.getElementById("index_top_user_span1").innerText = "游客";
            document.getElementById("index_top_user_span2").innerText = "去登陆";
            document.getElementById("home_username").innerText = "游客";
            document.getElementById("home_emil").innerText = "情去登录";
        }
    })
}


/**
 * 点击历史博客加载历史博客
 */
function BindHis() {

    document.querySelector('#history_btn').onclick = function () {
        let hisftooer = 2;
        let add_his = ``;
        add_his = myHistory(0, 5, add_his)
        if (!detailsFalg) {
            window.onscroll = function () {
                if (getScrollTop() + getClientHeight() == getScrollHeight()) {
                    hisftooer += 3;
                    add_his = myHistory(hisftooer, 3, add_his)
                    //加载到底部时查询

                }
            }
        }
    }

}


/**
 * 主文章渲染
 */
function indexArt() {
    let add_all = ``;

    let footer = 2;
    add_all = getAll(0, 5, add_all)
    if (!detailsFalg) {
        window.onscroll = function () {
            if (getScrollTop() + getClientHeight() == getScrollHeight()) {
                //加载到底部时查询
                footer += 3;
                add_all = getAll(footer, 5, add_all)
            }
        }
    }
}

/**
 * 模糊查询前端函数封装
 * @param value
 * @constructor
 */
function SearchArt(value, index, yenum) {
    let footer = 2;
    let add_sear = ``;
    let search_text = value;
    add_sear = search_Centent(search_text, index, yenum, add_sear)
    if (!detailsFalg) {
        window.onscroll = function () {
            if (getScrollTop() + getClientHeight() == getScrollHeight()) {
                //加载到底部时查询
                footer += 3;
                add_sear = search_Centent(search_text, footer, 3, add_sear)
            }
        }

    }
}

/**
 * 绑定回车查询函数
 * @type {number}
 */
//回车事件绑定
function BindSearchKeyup() {
    $('#sear_input').bind('keyup', function (event) {
        if (event.keyCode == "13") {
            SearchArt(this.value, 0, 5)
        }
    });

}

/**
 * 模糊搜索且分页渲染
 * @param text
 * @param index
 * @param yenum
 * @param add_sear 用来渲染清空标记
 */
function search_Centent(text, index, yenum, add_sear) {
    future.ajax({
        url: '/searchCent',
        type: 'get',
        data: {
            text,
            index,
            yenum
        },
        success: function (result) {
            if (result.length == 0) {
                alert("已经没有更多啦")
            } else {
                add_sear = add_searchList(result, add_sear)
            }
            if (index != 0 && result.length != 0) {
                alert("正在加载下一页")
            }
            //绑定图片放大功能效果
            BindBlowupImg()
            //绑定移动到文章上样式改变
            BindBlowupArt()

        }
    })
    return add_sear
}

/**
 * 获取所有分页渲染
 * @param text
 * @param index
 * @param yenum
 * @param add_sear 用来渲染清空标记
 */
function getAll(index, yenum, add_sear) {
    future.ajax({
        url: '/article/getAll',
        type: 'get',
        data: {
            index,
            yenum
        },
        success: function (result) {
            if (result.length == 0) {
                alert("已经没有更多啦")
            } else {
                add_sear = add_searchList(result, add_sear)
            }
            if (index != 0 && result.length != 0) {
                alert("正在加载下一页")
            }
            //绑定图片放大功能效果
            BindBlowupImg()
            //绑定移动到文章上样式改变
            BindBlowupArt()

        }
    })
    return add_sear
}


/**
 * 我的历史博客且分页渲染
 * @param num
 */
function myHistory(index, yenum, add_his) {
    future.ajax({
        url: '/article/myHistory',
        type: 'get',
        data: {
            num: index,
            yenum
        },
        success: function (result) {
            if (result.length == 0) {
                alert("已经没有更多啦")
            } else {
                add_his = add_searchList(result, add_his)
            }
            if (index != 0 && result.length != 0) {
                alert("正在加载下一页")
            }
            //绑定图片放大功能效果
            BindBlowupImg()
            //绑定移动到文章上样式改变
            BindBlowupArt()
        }
    })
    return add_his;
}


/**
 * 博客渲染到index,搜索与个人历史博客通用
 * @param result
 */
function add_searchList(result, add_sear) {
    result.forEach((item, index) => {
        item.time = rTime(item.time)
        //拼接
        if (item.thumbnail) {
            item.thumbnail = JsonstrToArr(item.thumbnail)
            add_sear +=
                `
              <li onclick="goDetails(this)" article_id=` + item.id + ` class="List_Art">
                        <div class="left_li">
                            <div class="left_li_img" >
                                <img src="` + item.thumbnail[0] + `" alt="" class="left_li_img_x">
                            </div>
`
        } else {
            add_sear += `
            <li onclick="goDetails(this)" article_id=` + item.id + `>
                        <div class="left_li">
                            <div class="left_li_img" >
                                <img src="../img/art1.jpg" alt="" class="left_li_img_x">
                            </div>`
        }
        add_sear += `
                    <div class="left_li_text">
                                <span>` + item.title + `</span>
                                <div class="li_p">
                                    <p>
                                     ` + item.contentText + `
                                    </p>
                                </div>
                                <div class="left_li_text_i">
                                    <x class="iconfont icon-riqi"></x>
                                    <i>` + item.time + `</i>
                                    <y class="iconfont icon-chakan"></y>
                                    <i>` + item.hits + `</i>
                                </div>
                            </div>
                        </div>
                    </li>
            `
    })
    document.querySelector('#index_content_left_ul').innerHTML = add_sear;
    return add_sear;
}


/**
 * 主页面热门文章渲染
 * @param num 分页数
 */
function gethot_onc(num) {
    future.ajax({
        url: '/getHot',
        type: 'get',
        data: {
            num,
        },
        success: function (result) {
            add_hotsList(result)
        }
    })
}


/**
 * 首页热门博客渲染
 * @param result
 */
function add_hotsList(result) {
    let add_hot = ``;
    result.forEach((item, index) => {
        //拼接
        add_hot += `
        <li onclick="goDetails(this)" article_id=` + item.id + ` >
          <a  >` + item.title + `</a>
          </li>
        `
    })
    document.querySelector('#content_right_list_ul').innerHTML = add_hot
}

/**
 * json字符串转数组
 * @param str
 * @returns {any}
 * @constructor
 */
function JsonstrToArr(str) {
    let arr = str.split(',');
    let s = JSON.parse(arr)
    return s;
}

/**
 * 转换时间格式
 * @param date
 * @returns {string}
 */
function rTime(date) {
    var json_date = new Date(date).toJSON();
    return new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}
